<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 *
 * @package     Real Estate CMS Pro
 * @author      Nurul Amin Muhit
 * @email       muhit18@gmail.com
 */
class Payments_m extends CI_Model {

    protected $_table = 'payments';

    public function __construct() {
        parent::__construct();
    }

    public function get_all() {
        $this->db
                ->select('payments.*')
                ->order_by('payments.id', 'desc');

        return $this->db->get($this->_table)->result();
    }

    public function get($id) {
        $this->db
                ->select('payments.*')
                ->where('payments.id', $id);

        return $this->db->get($this->_table)->row();
    }

    public function get_by($key, $value = '') {
        $this->db
                ->select('payments.*');

        if (is_array($key)) {
            $this->db->where($key);
        } else {
            $this->db->where($key, $value);
        }

        return $this->db->get($this->_table)->row();
    }

    public function get_many_by($params = array()) {

        if (!empty($params['key'])) {
            $this->db->where('payments.key', $params['key']);
        }

        if (!empty($params['value'])) {
            $this->db->where('payments.value', $params['value']);
        }

        return $this->get_all();
    }

    public function insert($input) {
        return $this->db->insert($this->_table, $input);
    }

    public function update($id, $input) {
        return $this->db->update($this->_table, array('payments.id' => $id), $input);
    }

    public function count_by($key, $value = '') {

        if (is_array($key)) {
            $this->db->where($key);
        } else {
            $this->db->where($key, $value);
        }

        return $this->db->count_all_results($this->_table);
    }

    public function count_all() {
        return $this->db->count_all_results($this->_table);
    }

    public function check_exists($field, $value = '', $id = 0) {
        if (is_array($field)) {
            $params = $field;
            $id = $value;
        } else {
            $params[$field] = $value;
        }
        $params['payments.id !='] = (int) $id;
        
        $this->db->where($params);

        return $this->db->count_all_results($this->_table) == 0;
    }

    /* OLD METHODS */

    function payments() {
        $this->db->join('users', 'users.id = payments.user');
        $query = $this->db->get('payments');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    function memberships() {
        $query = $this->db->get('memberships');
        if ($query->num_rows() > 0) {
            return $query->result_array();
        }
    }

    public function get_payable($id) {
        return $this->db->select('amount')
                        ->from('memberships')
                        ->where('m_id', $id)
                        ->get()
                        ->row()->amount;
    }

    public function get_user($username) {
        return $this->db->select('id')
                        ->from('users')
                        ->where('username', $username)
                        ->get()
                        ->row()->id;
    }

    function package_info($package = 'NULL') {
        $this->db->where('m_id', $package);
        $query = $this->db->get('memberships');
        if ($query->num_rows() > 0) {
            return $query->row_array();
        }
    }

    function expiry($user = NULL) {
        $this->db->where('user_id', $user);
        $query = $this->db->get('profiles');
        if ($query->num_rows() > 0) {
            $row = $query->row_array();
            return $row['membership_expiry'];
        }
    }

    public function membership_name($membership) {
        return $this->db->select('membership')
                        ->from('memberships')
                        ->where('m_id', $membership)
                        ->get()
                        ->row()->membership;
    }

}

/* End of file payments_m.php */
/* Location: ./application/models/payments_m.php */